import { Component } from '@angular/core';
import { NgForOf } from '@angular/common';
import { Student } from './model/student';
import { StudentService } from './service/student.service';
import { Studentvo } from './vo/studentvo';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})


export class AppComponent {
  //变量类型声明
testvalue:String;
numbervalue:Number;
student:Student;  
sl:Array<Student>;

studentvo:Studentvo;

constructor(private studentservice:StudentService){

}

  title = 'app';
//初始化方法
ngOnInit() {
  this.testvalue='    今天天气很好  abcdd';
  this.numbervalue=58+76;

  this.studentvo=new Studentvo();


  this.student=new Student();
  this.sl=new Array();

  this.loadStudentList();



  //this.student.name="测试姓名";
  //this.student.sex="男";
  //this.student.stunum="20161585484";
  //this.student.birthday=new Date().getTime();
  //this.student.nativeplace="测试籍贯";\

  //this.studentservice.getAnObject()
  //.then((data:any)=>{
  //  this.student=data;
  //})

}

//添加事件
add(){
  console.dir(this.studentvo);

  this.studentservice.addStudentInfo(this.studentvo)
  .then((data:any)=>{
    //请求事件完成后
    this.studentservice.getAnObject()
    .then((data:any)=>{
      this.student=data;
    })

  })




}

//载入student数据列表
loadStudentList(){
  this.studentservice.getStudentList()
  .then((data:any)=>{
     this.sl=data;

  })


}

//获得单个Student对象
getToUpdateStudent(id:string){
  this.studentservice.getSingleStudent(id)
  .then((data:Student)=>{
    this.studentvo=new Studentvo();
    this.studentvo.studentid=data.studentid;
    this.studentvo.xh=data.stunum;
    this.studentvo.xm=data.name;
  })
  
}
//删除单个对象
delteStudent(id:string){
  
  this.studentservice.deleteStudent(id)
  .then((flag:boolean)=>{
    if(flag==true){
      this.loadStudentList();
    }
    else{
      alert("操作异常，请刷新后重试");
    }
  })



}



}